package com.project.les;


import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.Toast;
import android.widget.TabHost.TabSpec;

import com.project.les.view.elements.FeriadoViewElement;
import com.project.les.view.elements.OrdenaFeriados;

public class TabsViewActivity extends TabActivity {
	private LocationManager lm;
	private LocationListener locationListener;

	private String locationName = "Campina Grande";
 
	private TabHost mTabHost;
 
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// GPS:
		// lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		// locationListener = new MyLocationListener();
		// lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
		// locationListener);

		setContentView(R.layout.tabs);
		
		mTabHost = (TabHost) findViewById(android.R.id.tabhost);
		//mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);
 
		updateFields();
	}
 
	private void updateFields() {
 
		Intent intent = null;
		int index = mTabHost.getCurrentTab();
		mTabHost.setCurrentTab(0);
		mTabHost.clearAllTabs();
 
		int[] resIDs = {
				R.drawable.nacional,
				R.drawable.regional,
				R.drawable.gps,
				R.drawable.feriadao,
				R.drawable.sobre,
		};
		
		for (int i = 1; i <= 5; ++i) {
			if (i == 1) {
				intent = new Intent().setClass(this, NacionaisListActivity.class);
				intent.putExtra("i", i);
				View tabview = createTabView(mTabHost.getContext(), resIDs[i-1]);
				
				tabview.setOnTouchListener(new OnTouchListener() {
					//@Override
					public boolean onTouch(View v, MotionEvent event) {
						if (event.getAction() == MotionEvent.ACTION_DOWN) {
							mTabHost.setCurrentTab(0);
							updateFields();
						}
						return true;
					}
				});
				 
				TabSpec setContent = mTabHost.newTabSpec("" + i).setIndicator(tabview)
						.setContent(intent);
				mTabHost.addTab(setContent);
				
			} else if (i == 2) {
				intent = new Intent().setClass(this, StateListActivity.class);
				intent.putExtra("i", i);
				View tabview = createTabView(mTabHost.getContext(), resIDs[i-1]);
				
				tabview.setOnTouchListener(new OnTouchListener() {
					//@Override
					public boolean onTouch(View v, MotionEvent event) {
						if (event.getAction() == MotionEvent.ACTION_DOWN) {
							mTabHost.setCurrentTab(1);
							updateFields();
						}
						return true;
					}
				});
				 
				TabSpec setContent = mTabHost.newTabSpec("" + i).setIndicator(tabview)
						.setContent(intent);
				mTabHost.addTab(setContent);
				
			} else if (i == 3) {

				intent = new Intent().setClass(this, LocaisListActivity.class);
				intent.putExtra("i", i);
				View tabview = createTabView(mTabHost.getContext(), resIDs[i-1]);
				
				tabview.setOnTouchListener(new OnTouchListener() {
					//@Override
					public boolean onTouch(View v, MotionEvent event) {
						if (event.getAction() == MotionEvent.ACTION_DOWN) {
							mTabHost.setCurrentTab(2);
							updateFields();
						}
						return true;
					}
				});
				 
				TabSpec setContent = mTabHost.newTabSpec("" + i).setIndicator(tabview)
						.setContent(intent);
				mTabHost.addTab(setContent);
				
			} else if (i == 4) {

				intent = new Intent().setClass(this, FeriadaoListActivity.class);
				intent.putExtra("i", i);
				View tabview = createTabView(mTabHost.getContext(), resIDs[i-1]);
				
				tabview.setOnTouchListener(new OnTouchListener() {
					//@Override
					public boolean onTouch(View v, MotionEvent event) {
						if (event.getAction() == MotionEvent.ACTION_DOWN) {
							mTabHost.setCurrentTab(3);
							updateFields();
						}
						return true;
					}
				});
				 
				TabSpec setContent = mTabHost.newTabSpec("" + i).setIndicator(tabview)
						.setContent(intent);
				mTabHost.addTab(setContent);
				
			} else {

				intent = new Intent().setClass(this, AboutActivity.class);
				intent.putExtra("i", i);
				View tabview = createTabView(mTabHost.getContext(), resIDs[i-1]);
				
				tabview.setOnTouchListener(new OnTouchListener() {
					//@Override
					public boolean onTouch(View v, MotionEvent event) {
						if (event.getAction() == MotionEvent.ACTION_DOWN) {
							mTabHost.setCurrentTab(4);
							updateFields();
						}
						return true;
					}
				});
				 
				TabSpec setContent = mTabHost.newTabSpec("" + i).setIndicator(tabview)
						.setContent(intent);
				mTabHost.addTab(setContent);
				
			} 
 
			//tabview.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_bg_selector));
 
		}
		mTabHost.setCurrentTab(index);
	}
 
	private static View createTabView(final Context context, int resId) {
		View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg_view, null);
		ImageView tv = (ImageView) view.findViewById(R.id.imgvTabImg);
		tv.setImageResource(resId);
		return view;
	}

	private void locationUpdate(Location loc) {
		double latitude = loc.getLatitude();
		double longitude = loc.getLongitude();

		Geocoder geocoder = new Geocoder(this, Locale.getDefault());

		// TextView myAddress = ((TextView) findViewById(R.id.edFeriados));

		try {
			List<Address> addresses = geocoder.getFromLocation(latitude,
					longitude, 1);

			if (addresses != null) {
				Address returnedAddress = addresses.get(0);
				StringBuilder strReturnedAddress = new StringBuilder();
				for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {
					strReturnedAddress
							.append(returnedAddress.getAddressLine(i)).append(
									"\n");
				}

				this.locationName = strReturnedAddress.toString();

			} else {
				this.locationName = "No Address returned!";
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			this.locationName = "Cannot get Address!";
		}
	}

	private class MyLocationListener implements LocationListener {

		public void onLocationChanged(Location loc) {
			if (loc != null) {
				locationUpdate(loc);
			}
		}

		public void onProviderDisabled(String provider) {
			// TODO Auto-generated method stub
		}

		public void onProviderEnabled(String provider) {
			// TODO Auto-generated method stub
		}

		public void onStatusChanged(String provider, int status, Bundle extras) {
			// TODO Auto-generated method stub
		}
	}

	

}